/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-04-28
 * Time: 20:54
 */
import java.util.Random;

class GenerateRandom implements Runnable {
    int[] arr = new int[10];  //缓冲区1
    Random random = new Random();
    int arrSize = 0;

    @Override
    public void run() {
        while (true) {
            try {
                if (arrSize == 10) {
                    Thread.sleep(2000);
                }
                arr[arrSize++] = random.nextInt();
                System.out.println(Thread.currentThread().getName() + " 成功生成了一个随机数");
                Thread.sleep(1000);
            } catch(Exception e) {
                e.printStackTrace();
            }
            if (arrSize != 0) {  //缓冲区1不为空时
                int toResolve = arr[arrSize--]; //取出等待分解的质因数
            }
        }
    }
}

class PrintThread implements Runnable {

    ResolveThread resolveThread;
    Random random = new Random();
    //将生成随机数的对象传入
    PrintThread(ResolveThread resolveThread) {
        this.resolveThread = resolveThread;
    }
    @Override
    public synchronized void run() {
        int index = random.nextInt(5); //生成一个随机下标
        try {
            if (resolveThread.arrSize == 0) {
                Thread.sleep(1000);
            } else {
                for (int i = 0; resolveThread.arr[index][i] != 0; i++) {
                    System.out.print(resolveThread.arr[index][i]);
                    if (i == 0) {
                        System.out.print("=");
                    } else if (resolveThread.arr[index][i + 1] != 0) {
                        System.out.print("+");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class ResolveThread implements Runnable {
    int[][] arr = new int[5][20];//缓冲区2
    int arrSize = 0;
    Random random = new Random();
    GenerateRandom generateRandom;
    ResolveThread(GenerateRandom generateRandom) {
        this.generateRandom = generateRandom;
    }
    @Override
    public void run() {
        while (true) {
            try {
                if (generateRandom.arrSize == 0) {
                    Thread.sleep(1000);
                } else {
                    int index = random.nextInt(10);
                    int m = generateRandom.arr[index];
                    int n = m;
                    for (int j = 2;j < m/2 && arrSize < 5; j++)  {   //从小到大寻找n的因数
                        int k = 0;
                        while(n % j == 0)   //当n%j（因数）== 0时，继续分解
                        {
                            this.arr[arrSize][k++] = j;
                            n /= j;
                        }
                        this.arrSize++;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
public class Practice2 {
    public static void main(String[] args) {
        GenerateRandom generateRandom = new GenerateRandom();//生成随机数的线程
        ResolveThread resolveThread = new ResolveThread(generateRandom);//分解因数线程
        PrintThread printThread = new PrintThread(resolveThread);  //打印线程2

        new Thread(generateRandom, "随机线程").start();
        new Thread(resolveThread, "分解线程1").start();
        new Thread(resolveThread, "分解线程2").start();
        new Thread(resolveThread, "分解线程3").start();
        new Thread(printThread, "打印线程1").start();
        new Thread(printThread, "打印线程2").start();
    }
}
